package com.service;

import com.bean.User;
import com.dao.BaseUserDao;
import com.dao.impl.UserDaoMysql;
import com.exception.DuplicateCodeException;
import com.exception.DuplicateUserPhoneException;
import com.util.RandomUtil;

import java.util.List;
import java.util.Map;

public class UserService {
    BaseUserDao baseUserDao=new UserDaoMysql();
    public List<Map<String, Integer>> console(){
        return baseUserDao.console();
    }

    public List<User> findAll(boolean limit, int offset, int pageNumber){
        return baseUserDao.findAll(limit,offset,pageNumber);
    }

    public User findByPhone(String userPhone){
        return baseUserDao.findByPhone(userPhone);
    }

    //Express findByCode(String code);

    //List<Express> findByUserPhone(String userPhone);

    //List<Express> findBySysPhone(String sysPhone);

    public boolean insert(User user){
        try {
            boolean flag = baseUserDao.insert(user);
            if(flag){
                //SMSUtil.send(e.getUserPhone(), e.getCode());
            }
            return flag;
        } catch (DuplicateUserPhoneException duplicateUserPhoneException) {
            return false;
        }
    }

    public boolean insert(String userPhone){
        try {
            boolean flag = baseUserDao.insert(userPhone);
            if(flag){
                //SMSUtil.send(e.getUserPhone(), e.getCode());
            }
            return flag;
        } catch (DuplicateUserPhoneException duplicateUserPhoneException) {
            return false;
        }
    }

    public boolean update(int id, User user){
        try {
            boolean flag = baseUserDao.update(id,user);
            if(flag){
                //SMSUtil.send(e.getUserPhone(), e.getCode());
            }
            return flag;
        } catch (DuplicateUserPhoneException duplicateUserPhoneException) {
            return false;
        }
    }

    public boolean updateLastLoginTime(String userPhone) {
        return baseUserDao.updateLastLoginTime(userPhone);
    }

    public boolean delete(int id){
        return baseUserDao.delete(id);
    }
}
